package org.dromara.timemate.service.impl;

import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.dromara.common.core.constant.HttpStatus;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.timemate.domain.DeviceInfo;
import org.dromara.timemate.domain.bo.DataIndexBo;
import org.dromara.timemate.domain.bo.DeviceBo;
import org.dromara.timemate.domain.vo.DataIndexVo;
import org.dromara.timemate.domain.vo.DeviceVo;
import org.dromara.timemate.mapper.DeviceInfoMapper;
import org.dromara.timemate.service.HomeService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import jakarta.annotation.Resource;

/**
 * 首页Service业务层处理
 *
 * @author Lion Li
 * @date 2021-07-26
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Resource
    private DeviceInfoMapper baseMapper;


    /*
     * 查询设备列表信息
     * */
    @Override
    public TableDataInfo<DeviceVo> queryPageDeviceList(String deviceType, String deviceName,  PageQuery pageQuery) {

        // 登录用户id
        Long userId = LoginHelper.getUserId();
        LambdaQueryWrapper<DeviceInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(userId != null, DeviceInfo::getUserId, userId);
        lqw.eq(StringUtils.isNotBlank(deviceType), DeviceInfo::getDeviceType, deviceType);
        lqw.like(StringUtils.isNotBlank(deviceName), DeviceInfo::getDeviceName, deviceName);
        Page<DeviceInfo> page = baseMapper.selectPage(pageQuery.build(), lqw);

        List<DeviceInfo> records = page.getRecords();
        // jdk 8 + lambda 表达式
        List<DeviceVo> retList = records.stream().map(new Function<DeviceInfo, DeviceVo>() {
            @Override
            public DeviceVo apply(DeviceInfo e) {
                DeviceVo deviceVo = new DeviceVo();
                deviceVo.setId(e.getId());
                deviceVo.setUserId(e.getUserId());
                deviceVo.setDeviceType(e.getDeviceType());
                deviceVo.setDeviceStatus(e.getDeviceStatus());
                deviceVo.setDeviceName(e.getDeviceName());
                return deviceVo;
            }
        }).collect(Collectors.toList());

        Page<DeviceVo> retPage = new Page<>();
        retPage.setRecords(retList);
        retPage.setTotal(page.getTotal());
        retPage.setSize(page.getSize());
        retPage.setCurrent(page.getCurrent());
        return TableDataInfo.build(retPage);
    }

    /*
     * 添加设备信息
     * */
    @Override
    public Boolean addDeviceInfo(DeviceBo deviceBo) {
        String tenantId = LoginHelper.getTenantId();
        Long userId = LoginHelper.getUserId();
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeviceType(deviceBo.getDeviceType());
        deviceInfo.setDeviceName(deviceBo.getDeviceName());
        deviceInfo.setUserId(userId);
        deviceInfo.setWearerId(deviceBo.getWearerId());
        deviceInfo.setTenantId(tenantId);
        if (baseMapper.insert(deviceInfo) > 0) {
            return true;
        }
        return false;
    }

    /*
     * 删除设备信息
     * */
    @Override
    public Boolean deleteWithValidByIds(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    /*
     * 查询指标数据信息
     * */
    @Override
    public TableDataInfo<DataIndexVo> queryPageDataIndexList(DataIndexBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        String deviceType = bo.getDeviceType();
        String deviceName = bo.getDeviceName();
        Long wearerId = bo.getWearerId();
        Date recordDay = bo.getRecordDay();
        Page<DataIndexVo> page = pageQuery.build();

        Integer pageNum = (int) page.getCurrent();
        Integer pageSize = (int) page.getSize();
        Integer start = (pageNum - 1) * pageSize;

        List<DataIndexVo> dataIndexVo = baseMapper.queryDataIndexList(deviceType,userId, wearerId, deviceName, recordDay, start, pageSize);
        long total = baseMapper.selectDataIndexCount(deviceType,userId, wearerId, deviceName, recordDay);
        TableDataInfo<DataIndexVo> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setRows(dataIndexVo);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setTotal(total);
        return tableDataInfo;
    }


}
